﻿using ManyToManyEntityFramework.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ManyToManyEntityFramework.Controllers
{
    public class StudentController : Controller
    {
        // GET: Student
        public ActionResult Index()
        {
            List<StudentIndexViewModel> model;
            using (ApplicationDbContext db = new ApplicationDbContext()) 
            {
                //Trying to select the student in a new instance of the class we created
                model = db.Students.Select(s => new StudentIndexViewModel() 
                {
                    //Get all the values of the students and put it into list
                    StudentId = s.StudentId,
                    Name = s.Name,
                    grade = s.Grade,
                    StudentCourses = s.Students_Courses.Select(c => new CourseViewModel() 
                    {
                       CourseId = c.Course.CourseId,
                       Name = c.Course.Name,
                       NumCredits = c.Course.NumCredits
                    }).ToList()
                }).ToList();
            }
            return View(model);
        }
    }
}